package com.young.common.util;


import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 公共工具类
* @ClassName: CommonUtil 
* @Description: 公共工具类
* @author rookie
* @date 2016-03-29 
*
 */
public class CommonUtil {

    //本机ip缓存,linux获取网卡配置缓慢(5S左右)
    private static String LOCAL_IP = null;

	/**
	 * 将request中的参数封装为map对象返回
	* @Title: getParameterFromRequest 
	* @Description: 将request中的参数封装为map对象返回
	* @param @param request
	* @param @return    设定文件 
	* @return Map<String,Object>    返回类型 
	* @throws
	 */
	public static Map<String,Object> getParameterFromRequest(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		String  tmp = request.getParameter("parameter");
        if(StringUtils.isNotBlank(tmp)){
            Map map = CommonJsonUtil.jsonStr2Bean(tmp, Map.class);
            result.putAll(map);
        }

		Enumeration<String> enums = request.getParameterNames();
		while (enums.hasMoreElements()) {
		    String key = enums.nextElement();
            String val = request.getParameter(key);
            result.put(key, val);
		}
		result.remove("parameter");
		result.remove("currentPage");
		result.remove("pageSize");
		return result;
	}

    /**
     * 获取上传文件的集合
     * @param request
     * @return
     */
    public static List<MultipartFile> getMultipartFileFromRequest(DefaultMultipartHttpServletRequest request){
        List<MultipartFile> list = new ArrayList<MultipartFile>();
        MultiValueMap<String, MultipartFile> map = request.getMultiFileMap();
        if (map != null){
            Set<String> keys = map.keySet();
            if (keys != null && keys.size() > 0){
                for (String key : keys){
                    List<MultipartFile> files = map.get(key);//注意:这里的get得到的是一个list
                    for(MultipartFile one : files){
                        if (one != null && !one.isEmpty()){
                            list.add(one);
                        }
                    }
                }

            }
        }
        return list;
    }
	
	/**
	 * 分页信息
	 * @param request
	 * @return
	 */
	public static Page<Map<String,Object>> getPageFromRequest(HttpServletRequest request){
        Page<Map<String,Object>> page = new Page<Map<String,Object>>();
        String currentPage = request.getParameter("pageNumber");
        page.setPageNumber(StringUtils.isNotBlank(currentPage)?Integer.parseInt(currentPage):1);
        String pageSize = request.getParameter("pageSize");
        page.setPageSize(StringUtils.isNotBlank(pageSize)?Integer.parseInt(pageSize):10);
        page.setCallback(StringUtils.isNotBlank(request.getParameter("callback"))?request.getParameter("callback"):"");
        return page;
    }
    
    /**
     * 获取UUID
     * @return
     * @author ccc
     * @date 2016年2月19日 下午3:45:58
     */
    public static String getUUID(){
    	return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获取默认值
     * @param str1
     * @param str2
     * @return
     */
    public static String getDefaultValue(String str1,String str2){
        if(StringUtils.isNotBlank(str1)){//str1 is not null
            return str1;
        }else{
            return str2;
        }
    }

    /**
     * 判断请求是否为ajax请求
     * @param request
     * @return
     */
    public static boolean isAjaxRequest(HttpServletRequest request){
        if (!(request.getHeader("accept")!=null && request.getHeader("accept").indexOf("application/json") > -1 ||
                request.getHeader("X-Requested-With")!=null && request.getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1)) {
            return false;
        }
        return true;
    }


}
